﻿#include "topo_loop.h"
#include "topo_coedge.h"

CAGD::TopoLoop::TopoLoop() : m_face(nullptr), m_coedge(nullptr), m_next(nullptr), m_dir(TopoDir::BOUNDARY)
{
}

CAGD::TopoLoop::~TopoLoop()
{
    // 移除下一条 loop
    if (m_next != nullptr)
        delete m_next;

    // 内部 loop 要移除所有 coedge 对
    if (m_dir == TopoDir::INTERIOR)
    {
        auto co = m_coedge;
        auto co2 = co;
        while (co != nullptr)
        {
            co = co->Next();
            delete co2->Twin();
            delete co2;
            co2 = co;

            if (co == m_coedge)
                break;
        }
    }
    // 边界 loop 移除 coedge 中的 loop 标记，以及前后连接
    else
    {
        auto co = m_coedge;
        auto co2 = co;
        while (co != nullptr)
        {
            co = co->Next();
            co2->Loop() = nullptr;
            co2->Prev() = nullptr;
            co2->Next() = nullptr;
            co2 = co;

            if (co == m_coedge)
                break;
        }
    }
}

CAGD::TopoFace *&CAGD::TopoLoop::Face()
{
    return m_face;
}

CAGD::TopoCoedge *&CAGD::TopoLoop::Coedge()
{
    return m_coedge;
}

CAGD::TopoLoop *&CAGD::TopoLoop::Next()
{
    return m_next;
}

CAGD::TopoDir CAGD::TopoLoop::Dir() const
{
    return m_dir;
}

CAGD::TopoType CAGD::TopoLoop::Type() const
{
    return TopoType::LOOP;
}
